//
//  CDEditBarToolsView.swift
//  PDF
//
//  Created by Kellv on 2024/4/19.
//

import UIKit

enum CDTextEditOptions: String {
    case redo = "Redo"
    case copy = "Copy"
    case doDocx = "To Docs"
    case export = "Export"
    //
    case a3 = "A3"
    case a4 = "A4"
    case a5 = "A5"
    case b4 = "B4"
    case b5 = "B5"
    //
    case noFooter = "No Footer"
    case leftSide = "Left Side"
    case center = "Center"
    case rightSide = "Right Side"
    
    case effect1 = "Effect_1"
    case effect2 = "Effect_2"
    case effect3 = "Effect_3"
    case effect4 = "Effect_4"
    case effect5 = "Effect_5"
    //
    case addpage = "Add Page"
    case crop = "Crop"
    case rotate = "Rotate"
    case effect = "Effect"
    case ocr = "OCR"
    case waterMark = "Watermark"
    case addText = "Add Text"
    case signature = "Signature"
    case encription = "Encription"
    case delete = "Delete"
    //other
    case pageSize
    case footer
    case no
}

class CDEditBarToolsView: UICollectionView,UICollectionViewDelegate,UICollectionViewDataSource {
    var types:[CDTextEditOptions] = []
    var selctIndex = -1
    private var collectionView: UICollectionView!
    var didSelectHanlder: ((CDTextEditOptions)->Void)?
    init(frame: CGRect, types:[CDTextEditOptions]) {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical

        layout.sectionInset = UIEdgeInsets(top: 8, left: 16, bottom: 8, right: 16)
        layout.minimumLineSpacing = 8
        
        layout.scrollDirection = .horizontal
        super.init(frame: frame, collectionViewLayout: layout)
        self.types = types
        layout.itemSize = CGSize(width: (self.width - 16 * 2 - 8 * 3)/4, height: 64)

        
        collectionView = UICollectionView(frame: self.bounds, collectionViewLayout: layout)
        collectionView.register(CDEditBarToolsTextAndImageCell.self, forCellWithReuseIdentifier: "CDEditBarToolsTextAndImageCell")
        collectionView.register(CDEditBarToolsImageCell.self, forCellWithReuseIdentifier: "CDEditBarToolsImageCell")

        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.backgroundColor = .white
        collectionView.showsHorizontalScrollIndicator = false
        self.addSubview(collectionView)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        types.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let option = types[indexPath.item]

        if option.rawValue.hasPrefix("Effect_") {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CDEditBarToolsImageCell", for: indexPath) as! CDEditBarToolsImageCell
            cell.imageView.image = option.rawValue.image
            if indexPath.item == selctIndex {
                cell.layer.borderColor = UIColor(hexStr: "4171FF").cgColor
                cell.layer.borderWidth = 2
            }else {
                cell.layer.borderWidth = 0

            }
            
            return cell
        }
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CDEditBarToolsTextAndImageCell", for: indexPath) as! CDEditBarToolsTextAndImageCell

        switch option {
        case .a3:
            cell.titelLabel.text = "297*420"
        case .a4:
            cell.titelLabel.text = "210*297"
        case .a5:
            cell.titelLabel.text = "148.5*210"
        case .b4:
            cell.titelLabel.text = "250*353"
        case .b5:
            cell.titelLabel.text = "176*250"
        default:
            cell.titelLabel.text = option.rawValue.localize()
        }
        cell.imageView.image = "\(option.rawValue)_tool".image

        
        if indexPath.item == selctIndex {
            cell.backgroundColor = UIColor(red: 0.918, green: 0.929, blue: 0.957, alpha: 1)
        }else {
            cell.backgroundColor = .white
        }
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.deselectItem(at: indexPath, animated: true)
        selctIndex = indexPath.item
        collectionView.reloadData()
        guard let didSelectHanlder = didSelectHanlder else {
            return
        }
        let option = types[indexPath.item]

        didSelectHanlder(option)
    }
     
    func loadData(options:[CDTextEditOptions]) {
        selctIndex = -1
        types = options
        collectionView.reloadData()
    }

}

class CDEditBarToolsTextAndImageCell: UICollectionViewCell {
    var imageView: UIImageView!
    var titelLabel: UILabel!
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.layer.cornerRadius = 8
        imageView = UIImageView()
        self.addSubview(imageView)
        imageView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.width.height.equalTo(26)
            make.top.equalToSuperview().offset(10)
        }
        
        titelLabel = UILabel()
        titelLabel.textColor = .black
        titelLabel.font = UIFont(name: "Helvetica-Bold", size: 14)
        titelLabel.textAlignment = .center
        titelLabel.text = "0/0"
        self.addSubview(titelLabel)

        titelLabel.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(imageView.snp.bottom).offset(8)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class CDEditBarToolsImageCell: UICollectionViewCell {
    var imageView: UIImageView!
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.layer.cornerRadius = 8
        imageView = UIImageView()
        self.addSubview(imageView)
        imageView.contentMode = .scaleAspectFill
        imageView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview().offset(3)
            make.bottom.right.equalToSuperview().offset(-3)
        }
        imageView.addRadius(corners: .allCorners, size: CGSize(width: 8, height: 8))
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
